using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class EntityDataUI : MonoBehaviour
{
    RectTransform myTransform;
    private EntityBase character;
    private CharacterData characterData;
    private Slider healthBar;

    // Start is called before the first frame update
    void Start()
    {
        myTransform = GetComponent<RectTransform>();
        character = GetComponentInParent<EntityBase>();
        characterData = GetComponentInParent<CharacterData>();
        healthBar = GetComponentInChildren<Slider>();
        if (character != null)
        {
            character.onFilpped += CheckHealthUIFilp;
        }
        if (characterData != null)
        {
            characterData.onHealthChanged += UpdateHealthUI;
            characterData.onCharacterDead += CloseHealthUI;
            UpdateHealthUI();
        }
        
    }

    private void OnDestroy()
    {
        characterData.onHealthChanged -= UpdateHealthUI;
        characterData.onCharacterDead -= CloseHealthUI;
    }

    private void CloseHealthUI()
    {
        healthBar.gameObject.SetActive(false);
    }

    private void CheckHealthUIFilp()
    {
        myTransform.Rotate(0, 180, 0); 
    }
  

    private void UpdateHealthUI()
    {
        healthBar.value = (float)characterData.currentHealth / (float)characterData.GetMaxHealthValue();
    }
}
